$(document).ready(function () {
    doGetAllUser();
});
function doGetAllUser(){
    $.post("doGetAllUser",function(result){
        if(result.state==1){
            var list=result.data;
            var $div=$(".col-sm-12");
            $div.empty();
            for(var i in list){
                $div.append(
                    ' <div class="col-sm-2 partWhite">'+
                    ' <div class="comHtmlTitle">'+
                    ' <div class="userTop"><a onclick=showMessage(\'确定要删除吗\',\'1\',\'deleteUser(\"'+list[i].userID+'\")\')>删除</a></div>'+
                    ' <div class="userInformation">'+
                    '  <img src="../PM/images/bai-yonghu.png"/>'+
                    '  <div class="userMessage">'+
                    ' <span class="userName"> '+list[i].userName+'</span><br/>'+
                    ' <span class="userId"> ID : <span>'+list[i].userID+'</span></span>'+
                    ' </div>'+
                    ' </div>'+
                    '</div>'+
                    ' </div>'
                );
            }
            $div.append('<div class="col-sm-2 partWhite" hidden="hidden" id="addUserIDs">'+
                ' <div class="comHtmlTitle">'+
                ' <div class="userTop"><a class="spanFloat" onclick="closeAddModule(\'addUserIDs\')">取消</a><a onclick="addUserInfo()">确定</a></div>'+
                ' <div class="userInformation">'+
                '  <img src="../PM/images/bai-yonghu.png"/>'+
                '  <div class="userMessage2">'+
                ' <input class="UserName" name="UserName" id="UserName" value="" placeholder="请输入用户名"/><br/>'+
                ' <input class="UserID" name="UserID" id="UserID" value="" placeholder="请输入ID"/>'+
                ' </div>'+
                ' </div>'+
                '</div>' +
                '</div>' +
                '<div class="col-sm-2 partWhite" id="userModule" onclick="showAddUser()">添加用户</div>');
        }else{
            showMessage("获取用户信息失败");
        }
    });
}
function closeAddModule(hideModel) {
    $("#" + hideModel).hide();
}
function showAddUser(){
    $("#addUserIDs").show();
}

function addUserInfo(){
     $.post("doGetAllUserID",{"userID":$("#UserID").val()},function(result){
               if(result.state == 1){
                   if(result.data == 0){
                   $.ajax({
                       url : "addUserInfo",
                       type : 'post',
                       dataType : 'json',
                       data: {
                           "UserID": $("#UserID").val(),
                           "UserName": $("#UserName").val(),
                       },
                       success : function(data) {
                           var result = data && data.result;
                           if (result == "Success") {
                               doGetAllUser();
                               $("#addUserIDs").hide();
                               showMessage("添加成功");
                           } else if (result == "NotFound") {
                               showMessage("添加失败");
                           }
                       },error : function(data) {
                           showMessage("添加失败");
                       },
                       timeout: function(data){
                           showMessage("添加超时");
                       }
                   })
                   }else{
                     showMessage("ID已经存在");
                   }
               }else{
                showMessage("添加失败");
               }
     })

}

function deleteUser(ID) {
    $.ajax({
        url : "updateUserInfo",
        type : 'post',
        dataType : 'json',
        data: {
            "UserID": ID,
        },
        success : function(data) {
            var result = data && data.result;
            if (result == "Success") {
                doGetAllUser();
                showHideModal();
                showMessage("删除成功");
            } else if (result == "NotFound") {
                showMessage("删除失败");
            }
        },error : function(data) {
            showMessage("删除失败");
        },
        timeout: function(data){
            showMessage("删除超时");
        }
    })
}
